package com.riaevolution.themes.MenuBar {

	import flash.display.GradientType;
	
	import mx.core.EdgeMetrics;
	import mx.core.UIComponent;
	import mx.skins.Border;
	import mx.skins.halo.HaloColors;
	import mx.styles.StyleManager;
	import mx.utils.ColorUtil;
	
	/**
	* The skin for the background of a MenuBar.
	*/
	
	public class MenuBarBackgroundSkin extends Border
	
	{
		/**
		* @private
		*/
		
		private static var cache:Object = {};
		/**
		* @private
		* Several colors used for drawing are calculated from the base colors
		* of the component (themeColor, borderColor and fillColors).
		* Since these calculations can be a bit expensive,
		* we calculate once per color set and cache the results.
		*/
		
		private static function calcDerivedStyles(themeColor:uint, fillColor0:uint, fillColor1:uint):Object
		{
			var key:String = HaloColors.getCacheKey(themeColor,	fillColor0, fillColor1);
			if (!cache[key])
			{
				var o:Object = cache[key] = {};
				// Cross-component styles.
				HaloColors.addHaloColors(o, themeColor, fillColor0, fillColor1);
			}
			return cache[key];
		}

		
		/**
		* Constructor.
		*/
		
		public function MenuBarBackgroundSkin()	{
			super();
		}

		/**
		* @private
		* Storage for the borderMetrics property.
		*/
		
		private var _borderMetrics:EdgeMetrics = new EdgeMetrics(0, 0, 0, 0);
		
		/**
		* @private
		*/
		
		override public function get borderMetrics():EdgeMetrics {
			return _borderMetrics;
		}
		/**
		* @private
		*/
		override public function get measuredWidth():Number {
			return UIComponent.DEFAULT_MEASURED_MIN_WIDTH;
		}
		
		/**
		* @private
		*/
		
		override public function get measuredHeight():Number {
			return UIComponent.DEFAULT_MEASURED_MIN_HEIGHT;
		}

		/**
		* @private
		*/
		
		override protected function updateDisplayList(w:Number, h:Number):void {
			
			super.updateDisplayList(w, h);
			var outsideACB:Boolean = true;
			if ( getStyle("translucent")) {
				outsideACB = false;
			}
			super.updateDisplayList(w, h);

			// User-defined styles.
			var bevel:Boolean = getStyle("bevel");
			var borderColor:uint = getStyle("borderColor");
			var cornerRadius:Number = getStyle("cornerRadius");
			var fillAlphas:Array = getStyle("fillAlphas");
			var fillColors:Array = getStyle("fillColors");
			StyleManager.getColorNames(fillColors);
			var themeColor:uint = getStyle("themeColor");
	
			// Derivative styles.
			var derStyles:Object = calcDerivedStyles(themeColor, fillColors[0],
													 fillColors[1]);
	
			var borderColorDrk1:Number =
				ColorUtil.adjustBrightness2(borderColor, -25);
			
			var cr:Number = Math.max(0, cornerRadius);
			var cr1:Number = Math.max(0, cornerRadius - 1);
			if(outsideACB) {
				fillAlphas = [1,1];
			}
			
	   		var upFillColors:Array = [ fillColors[0], fillColors[1] ];
	   		
			var upFillAlphas:Array = [ fillAlphas[0], fillAlphas[1] ];
													
			graphics.clear();
	
			// button border/edge
			if(outsideACB) {
			//drawRoundRect(x,y,w,h,cr,co,alpha,gradient,gtype,gratio,hole);
			drawRoundRect(
				0, 0, w, h, cr,
				[ borderColor, borderColorDrk1 ], 1); 
			}
			// button fill
			drawRoundRect(
				1, 1, w - 2, h - 2, cr1,
				upFillColors, upFillAlphas,
				verticalGradientMatrix(1, 1, w - 2, h - 2)); 
		}
	
	}

}